package net.dopan.pigframe.demo.service.impl;

import net.dopan.pigframe.core.base.service.impl.BaseServiceImpl;
import net.dopan.pigframe.demo.mapper.UserDemoMapper;
import net.dopan.pigframe.demo.model.entity.UserDemo;
import net.dopan.pigframe.demo.service.UserDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Package: net.dopan.pigframe.demo.service.impl
 * @Title: UserDemoServiceImpl
 * @Description: 用户信息
 * @author: 刘宽
 * @date: 2018/10/19 9:51
 */
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class UserDemoServiceImpl extends BaseServiceImpl<UserDemoMapper, UserDemo> implements UserDemoService {

    @Autowired
    private UserDemoMapper userDemoMapper;

    /**
     * 添加用户
     * @param user 用户model
     * @return true/false
     */
    @Override
    public Boolean addUser(UserDemo user) {
        userDemoMapper.insert(user);
        return true;
    }

    /**
     * 删除用户
     * @param id
     * @return true/false
     */
    @Override
    public Boolean deleteById(Long id) {
        userDemoMapper.deleteById(id);
        return true;
    }

    /**
     * 查询用户
     * @param id
     * @return 用户列表
     */
    @Override
    public UserDemo findById(Long id) {
        return userDemoMapper.selectById(id);
    }

    /**
     * 更新用户
     * @param user
     * @return true/false
     */
    @Override
    public Boolean myUpdateById(UserDemo user) {
        UserDemo updateUser = userDemoMapper.selectById(user.getId());
        if((null == user.getId()) && (null == updateUser.getId())) {
            return false;
        }
        else {
            userDemoMapper.updateById(user);
            return true;
        }
    }

    /**
     * 获取用户
     *
     * @return 所有用户列表
     */
    @Override
    public List<UserDemo> getUser() {
        return userDemoMapper.selectList(null);
    }

}
